//
//  YDBAppManager.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2018/8/8.
//  Copyright © 2018年 杨东宝. All rights reserved.
//

import UIKit
/*
 *实际的项目可能登录逻辑不同，根据自己实际需求去修改
 */
let KDevicesCacheName  = "KDevicesCacheName"
let appManger = YDBAppManager.sharedInstance

class YDBAppManager: NSObject {
    
    var appDelegate:AppDelegate?
    var sceneDelegate:SceneDelegate?
    var launchOptions:[UIApplication.LaunchOptionsKey: Any]?
    // 创建单例
    static let sharedInstance = YDBAppManager()
    
    var mainTabbar:YDBTabBarController?
    
    // 设置初始化方法为私有，防止报错
    private override init() {
        
    }
    
    // MARK:- ————————初始化用户数据————————
    func initUserInfor() {
        userManger.loadUserInfor()
        self.chooseRootVC()
    }
    //  MARK:- ————————选择根控制器————————
    func chooseRootVC() {
        if userManger.isLogined {
            self.jumpToMainVC()
            return
        }
        self.jumpToLoginVC()
    }
    //  MARK:- ————————跳转到主页————————
    func jumpToMainVC() {
        mainTabbar = YDBTabBarController()
        if (appDelegate?.window != nil) {
            appDelegate?.window?.rootViewController = mainTabbar
        }else{
            sceneDelegate?.window?.rootViewController = mainTabbar
        }
        
    }
    //  MARK:- ————————跳转到登录 销毁tabbar控制器————————
    func jumpToLoginVC() {
        mainTabbar = nil
        
        if (appDelegate?.window != nil) {
            appDelegate?.window!.rootViewController = YDBNavigationController(rootViewController: YDBLoginViewController())
        }else{
            sceneDelegate?.window?.rootViewController = YDBNavigationController(rootViewController: YDBLoginViewController())
        }
    }
    //  MARK:- ————————跳转到登录 不销毁tabbar控制器 present————————
    func PresentToLoginVC() {
        self.mainTabbar?.present(YDBNavigationController(rootViewController: YDBLoginViewController()), animated: true, completion: nil)
    }
    //MARK:- ————————获取最顶层控制器————————
    func getCurrentUIVC() -> UIViewController{
        
        let superVC = self.getCurrentVC()
        
        if superVC.isKind(of: UITabBarController.self){
            
            let  tabSelectVC = (superVC as! UITabBarController).selectedViewController;
            
            if tabSelectVC!.isKind(of: UINavigationController.self) {
                
                return (tabSelectVC as! UINavigationController).viewControllers.last!
            }
            return tabSelectVC!
        }else if superVC.isKind(of: UINavigationController.self) {
            
            return (superVC as! UINavigationController).viewControllers.last!
        }
        return superVC;
    }
    
    func getCurrentVC() -> UIViewController{
        
        var result:UIViewController?
        var window = UIApplication.shared.keyWindow!
        if window.windowLevel != UIWindow.Level.normal{
            let windows = UIApplication.shared.windows
            for temWind in windows {
                if temWind.windowLevel == UIWindow.Level.normal
                {
                    window = temWind
                    break
                }
            }
        }
        let frontView = window.subviews[0]
        
        let nextResponder = frontView.next
        
        if nextResponder!.isKind(of: UIViewController.self){
            result = nextResponder as? UIViewController;
        } else{
            result = window.rootViewController;
        }
        return result!
    }
    //  MARK:- ————————APP初始化————————
    func initAppWithApplication(appdelegate: AppDelegate? = nil,scenedelegate:SceneDelegate? = nil) {
        appDelegate = appdelegate
        sceneDelegate = scenedelegate
        self.initIQKeyBordManger()
        // 设置启动页面时间
        Thread.sleep(forTimeInterval: 2.0)
        // 初始化用户信息
        self.initUserInfor()
        self.registerAPNS(launchOptions: launchOptions)
    }
}

//MARK:- ————————初始化第三方数据———————
extension YDBAppManager:UNUserNotificationCenterDelegate{
    
    //MARK:- ————————初始化键盘管理————————
    func initIQKeyBordManger() {
        
        let keyboardManager = IQKeyboardManager.shared // 获取类库的单例变量
        
        keyboardManager.enable = true // 控制整个功能是否启用
        
        keyboardManager.shouldResignOnTouchOutside = true // 控制点击背景是否收起键盘
        
        keyboardManager.toolbarManageBehaviour = .bySubviews // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
        
        keyboardManager.enableAutoToolbar = true // 控制是否显示键盘上的工具条
        
        keyboardManager.keyboardDistanceFromTextField = 10.0 // 输入框距离键盘的距离
        
    }

    //MARK:- ————— 注册离线推送 —————
    func registerAPNS(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) {
        
        let os = ProcessInfo().operatingSystemVersion
        switch (os.majorVersion, os.minorVersion, os.patchVersion) {
        case (10, _, _):
            self.registerPush10()
        case (8,_, _):
            self.registerPush8to9()
        default:
            self.registerPushBefore8()
        }
        self.registuerJPUSH(launchOptions: launchOptions)
        
    }
    func registerPush10() {
        
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.delegate  = self;
            center.requestAuthorization(options:[.badge,.sound,.alert]) { (granted, error) in}
            UIApplication.shared.registerForRemoteNotifications()
        } else {
            
        };
        
    }
    func registerPush8to9() {
        
        let types:UIUserNotificationType = [.badge,.sound,.alert]
        
        
        let mySettings = UIUserNotificationSettings.init(types: types, categories: nil)
        
        UIApplication.shared.registerUserNotificationSettings(mySettings)
        
        UIApplication.shared.registerForRemoteNotifications()
        
    }
    
    func registerPushBefore8() {
        UIApplication.shared.registerForRemoteNotifications(matching: [.badge,.sound,.alert])
    }
    func registuerJPUSH(launchOptions:[UIApplication.LaunchOptionsKey: Any]?) {
        
        
    }
    
}
